package com.application.DataProvider;

import com.application.DTO.TypeTopicDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TypeTopicDAO {
Context _mcontext = null;
	
	public TypeTopicDAO(Context context){
		this._mcontext = context;
	}
	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from TypeTopic where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	public TypeTopicDTO getTypeTopic(int IDTypeTopic)
	{
		TypeTopicDTO tpDto = new TypeTopicDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from TypeTopic where id = " + IDTypeTopic;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				tpDto.set_ID(cur.getInt(0));
				tpDto.set_Name(cur.getString(1));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return tpDto;
	}
	
	public String getName(int id)
	{
		String result = "";
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Name from TypeTopic where ID = " + id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				result = cur.getString(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	
	//Insert du lieu vao bang TypeTopic
	public int insert(TypeTopicDTO ftDto) {
		Log.d("PEERDROID","insert TypeTopic");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", ftDto.get_ID());
			values.put("Name", ftDto.get_Name());
			
			result = provider.execInsertCommand(db, "TypeTopic", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	//Cap nhat bang TypeTopic
	public int update(String name, int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Name", name);
			
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};
			result= provider.execUpdateCommand(db, "TypeTopic", values, whereClause, whereArgs);
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	
	public int delete(int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result=provider.execDeleteCommand(db, "TypeTopic", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
}
